package com.ruoyi.web.controller.user;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.UserSettlement;
import com.ruoyi.web.service.IUserOrderService;
import com.ruoyi.web.service.IUserSettlementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 结算单Controller
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/admin/settlement")
public class UserSettlementController extends BaseController
{
    @Autowired
    private IUserSettlementService userSettlementService;

    @Autowired
    private IUserOrderService userOrderService;
    /**
     * 查询结算单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserSettlement userSettlement)
    {
        startPage();
        List<UserSettlement> list = userSettlementService.selectUserSettlementList(userSettlement);
        return getDataTable(list);
    }

    /**
     * 导出结算单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:export')")
    @Log(title = "结算单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserSettlement userSettlement)
    {
        List<UserSettlement> list = userSettlementService.selectUserSettlementList(userSettlement);
        ExcelUtil<UserSettlement> util = new ExcelUtil<UserSettlement>(UserSettlement.class);
        util.exportExcel(response, list, "结算单数据");
    }

    /**
     * 获取结算单详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userSettlementService.selectUserSettlementById(id));
    }

    /**
     * 新增结算单
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:add')")
    @Log(title = "结算单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserSettlement userSettlement)
    {
        // 将逗号分隔的订单ID字符串转换为Long数组
        Long[] orderIds = userSettlement.getOrderIds();
        int rows2 = userSettlementService.insertUserSettlement(userSettlement);
        int rows = 0;
        if(rows2>0){
            rows = userOrderService.updateUserOrderIsSettlementByOrderIds(orderIds, "2");
        }
        return toAjax(rows+rows2);
    }

    /**
     * 修改结算单
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:edit')")
    @Log(title = "结算单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserSettlement userSettlement)
    {
        return toAjax(userSettlementService.updateUserSettlement(userSettlement));
    }

    /**
     * 删除结算单
     */
    @PreAuthorize("@ss.hasPermi('admin:settlement:remove')")
    @Log(title = "结算单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userSettlementService.deleteUserSettlementByIds(ids));
    }
}
